/**
 * Created by bootdo.
 */
import axios from 'axios'
import store from "../vuex/store";
import {
  bus
} from '../bus.js'

axios.defaults.withCredentials = true;
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
//  axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';//配置请求头


//添加一个请求拦截器
axios.interceptors.request.use(
  config => {
    // 将token放入请求头
    if (window.localStorage.getItem('access-token')) {
      config.headers.Authorization = window.localStorage.getItem('access-token');
      // 将用户登陆信息放入请求头
      config.headers.Name = window.localStorage.getItem('name');
      config.headers.UserId = window.localStorage.getItem('userId');
    }

    store.state.loading = true
    return config
  },
  error => {
    return Promise.reject(error)
  }
);
// 添加一个响应拦截器
axios.interceptors.response.use(function (response) {
  store.state.loading = false
  if (response.data && response.data.code) {
    if (parseInt(response.data.code) === 401) {
      //未登录
      bus.$emit('goto', '/login')
    }
  }

  return response;
}, function (error) {
  store.state.loading = false
  // Do something with response error
  return Promise.reject(error);
});

//基地址
let base = process.env.API_ROOT


//通用方法
export const POST = (url, params) => {
  return axios.post(`${base}${url}`, params).then(res => res.data)
}

export const GET = (url, params) => {
  return axios.get(`${base}${url}`, {
    params: params
  }).then(res => res.data)
}
export const DOWNLOAD = (url, params) => {
  console.log('----------DOWNLOAD------------')
  // console.log(base + url)
  // console.log(params.name)
  return axios.get(`${base}${url}`, {
    headers: {Authorization: params.token},
    responseType: 'blob',
    params: {id: params.id}
  })
  // .then(res => {
  //   console.log("response: ", res);
  //   console.log('mydata',res.data)
  // })
}

export const GETLIST = (url, params) => {
  return axios.get(`${base}${url}${params.name}`, {
    headers : {Authorization: params.token},
  }).then(res => res.data)
}

export const PUT = (url, params) => {
  return axios.put(`${base}${url}`, params).then(res => res.data)
}

export const DELETE = (url, params) => {
  return axios.delete(`${base}${url}`, {
    params: params
  }).then(res => res.data)
}

export const PATCH = (url, params) => {
  return axios.patch(`${base}${url}`, params).then(res => res.data)
}

//上链
// export const CHAIN_POST = (url, params) => {
//   return axios.post(`${base}${url}`, params.chain.nothing, params.chain.key ,
//   {
//     headers:{Authorization: params.token},   
//   }).then(res => res.data)
// }
